// pages/family/family.js
const app = getApp();
const util = require('../../../utils/util.js');
const md5 = require('../../../utils/md5.js');
const call = require('../../../utils/request.js')
Page({
  data: {
    memberInfoId: '',
    sHeight: app.globalData.sHeight,
    userId: '',
    groupId: '',
    memberList: [],
    member: '',
    memberIndex: 0,
    currentMember: {},
    isOpenCode: false,
    _mobileCode: null,
    mobileCode: null,
    actionSheetItems: ['删除家庭'],
    isSetting: false,
    defaultUrl: 'http://pic1.unilifemedia.com/umanager/userfiles/1/images/user/headPicture/2018/07/Artboard%2022%20Copy%204.png',
    activeCard: 'member',
    recipeList: []
  },
  /**
   * 设置
   */
  onSettings(e) {
    console.log('hei')
    this.setData({
      isSetting: !this.data.isSetting
    })
  },
  actionSheetChange(e) {
    console.log('数据', e)
  },
  /**
   * 添加成员
   */
  onAppend() {
    wx.navigateTo({
      url: '/pages/family/member/edit/edit?addInfo=' + [this.data.userId, this.data.groupId],
    })
  },
  /**
   * 编辑成员
   */
  onEdit() {
    wx.navigateTo({
      url: '/pages/family/member/edit/edit',
    })
  },
  /**
   * 头像更换动作
   */
  onChangeMember(e) {
    // wx.vibrateShort();//震动
    const dataset = e.currentTarget.dataset;
    const index = dataset.action === 'touchmove' ? e.detail.current : dataset.index;
    this._filterRcipe(this.data.memberList[index].memberInfoId)
    this.setData({
      memberIndex: index,
      currentMember: this.data.memberList[index],
      // activeCard: 'member'
    })
  },
  /**
   * 输入验证码
   */
  subMobileCode(v) {
    var that = this;
    if (that.data._mobileCode) clearTimeout(that.data._mobileCode)
    this.setData({
      _mobileCode: setTimeout(function() {
        that.setData({
          mobileCode: v.detail.value
        })
      }, 500)
    })
  },
  closeCode() {
    this.setData({
      isOpenCode: false
    })
  },
  onCard(e) {
    this.setData({
      activeCard: e.currentTarget.dataset.action
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('onLoad')
    const id = options.id;
    this._filterRcipe(id);
  },
  onShow: function() {
    console.log('onShow')
    var that = this;
    that._getFamilyDetail(); //8)查询家庭群组信息（包括成员信息）
  },
  /**
   * 8)查询家庭群组信息（包括成员信息）
   */
  _getFamilyDetail() {
    console.log('开始查询')
    var that = this;
    const url = `/service/standard/v1/family/group/getFamilyDetail?access_token=${app.globalData.access_token}`;
    const params = {
      "header": app.globalData.reqHeader,
      "data": {
        "userId": that.data.userId
      }
    };
    console.log('调试1', params)
    call.postData(url, params,
      res => {
        console.log('查询成功');
        console.log('_getFamilyDetail_success', res);
        if (res.errormsg !== 'success') { //如果有异常状况，展示错误
          wx.showToast({
            title: res.errormsg,
            icon: 'none'
          })
        } else if (res.data.groupId) { //如果能查到群组Id，就展示页面
          const index = res.data.memberList.indexOf(res.data.memberList.find(i => i.memberInfoId == that.data.memberInfoId))
          this.setData({
            memberList: res.data.memberList,
            currentMember: res.data.memberList[index],
            groupId: res.data.groupId,
            memberIndex: index
          })
        }
      },
      err => {
        console.log('_getFamilyDetail_fail', err);
        wx.showToast({
          title: '网络好像开小差了哦~',
        })
      }, 'api')
  },
  _filterRcipe(id) {
    const data = getCurrentPages()[getCurrentPages().length - 2].data; //获取上一页的data
    const dailyRecommend = [data.recipes.breakfasts, data.recipes.lunchs, data.recipes.dinners];
    let dailyList = [];
    let dailyItem;
    for (let m = 0; m < dailyRecommend.length; m++) {

      dailyItem = dailyRecommend[m].filter(r => {
        return r.memberInfos.find(i => i.memberInfoId == id)
      });
      dailyList.push(dailyItem)
    }

    console.log('dailyList', dailyList)
    this.setData({
      memberInfoId: id,
      userId: data.userId,
      recipeList: dailyList
    })
  }
})